package org.esprit.ocm.server.metier.oca.impl;

import java.util.ArrayList;
import java.util.List;

import org.esprit.ocm.client.rpc.oca.VirtualNetworkOcaService;
import org.esprit.ocm.model.oca.VnetOca;
import org.esprit.ocm.server.metier.oca.api.Client;
import org.esprit.ocm.server.metier.oca.api.OneResponse;
import org.esprit.ocm.server.metier.oca.api.vnet.VirtualNetwork;
import org.esprit.ocm.server.metier.oca.api.vnet.VirtualNetworkPool;

public class VirtualNetworkOcaServiceImpl implements VirtualNetworkOcaService {

	/**
	 * List All vms
	 * 
	 * @param client
	 * @return
	 * @throws Exception
	 */
	public List<VnetOca> listAll(String secret, String endpoint, int filter) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualNetworkPool pool = new VirtualNetworkPool(client, filter);
		pool.info();

		List<VnetOca> l = new ArrayList<VnetOca>();

		for (VirtualNetwork from : pool) {
			VnetOca to = new VnetOca();

			to.setId(from.xpath("ID"));
			to.setUid(from.xpath("UID"));
			to.setUsername(from.xpath("USERNAME"));
			to.setName(from.xpath("NAME"));
			to.setType(from.xpath("TYPE"));
			to.setBridge(from.xpath("BRIDGE"));
			to.setPublicState(from.xpath("PUBLIC"));
			to.setTotal_leases(from.xpath("TOTAL_LEASES"));
			to.setTemplate_bridge(from.xpath("TEMPLATE/BRIDGE"));
			to.setTemplate_name(from.xpath("TEMPLATE/NAME"));
			to.setTemplate_type(from.xpath("TEMPLATE/TYPE"));
			to.setTemplate_description(from.xpath("TEMPLATE/DESCRIPTION"));
			to.setTemplate_network_address(from.xpath("TEMPLATE/NETWORK_ADDRESS"));
			to.setTemplate_network_size(from.xpath("TEMPLATE/NETWORK_SIZE"));
			to.setTemplate_leases_ip(from.xpath("TEMPLATE/LEASES/IP"));
			to.setLeases_lease_ip(from.xpath("LEASES/"));
			to.setLeases_lease_mac(from.xpath("LEASES/"));
			to.setLeases_lease_used(from.xpath("LEASES/"));
			to.setLeases_lease_vid(from.xpath("LEASES/"));

			to.setErrorMSG(from.info().getErrorMessage());
			l.add(to);
		}
		return l;
	}

	/**
	 * Create New vnet
	 * 
	 * @param client
	 * @param template
	 * @param image
	 * @return
	 * @throws Exception
	 */
	public String allocate(String secret, String endpoint, String template) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = VirtualNetwork.allocate(client, template);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Delete an vnet
	 * 
	 * @param client
	 * @param vnetId
	 * @throws Exception
	 */
	public String delete(String secret, String endpoint, int vnetId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = VirtualNetwork.delete(client, vnetId);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * Describe vnet information
	 * 
	 * @param client
	 * @param vnetId
	 * @return
	 * @throws Exception
	 */
	public VnetOca info(String secret, String endpoint, int vnetId) throws Exception {
		Client client = new Client(secret, endpoint);
		VirtualNetwork from = new VirtualNetwork(vnetId, client);
		
		VnetOca to = new VnetOca();
		to.setId(from.xpath("ID"));
		to.setUid(from.xpath("UID"));
		to.setUsername(from.xpath("USERNAME"));
		to.setName(from.xpath("NAME"));
		to.setType(from.xpath("TYPE"));
		to.setBridge(from.xpath("BRIDGE"));
		to.setPublicState(from.xpath("PUBLIC"));
		to.setTotal_leases(from.xpath("TOTAL_LEASES"));
		to.setTemplate_bridge(from.xpath("TEMPLATE/BRIDGE"));
		to.setTemplate_name(from.xpath("TEMPLATE/NAME"));
		to.setTemplate_type(from.xpath("TEMPLATE/TYPE"));
		to.setTemplate_description(from.xpath("TEMPLATE/DESCRIPTION"));
		to.setTemplate_network_address(from.xpath("TEMPLATE/NETWORK_ADDRESS"));
		to.setTemplate_network_size(from.xpath("TEMPLATE/NETWORK_SIZE"));
		to.setTemplate_leases_ip(from.xpath("TEMPLATE/LEASES/IP"));
		to.setLeases_lease_ip(from.xpath("LEASES/"));
		to.setLeases_lease_mac(from.xpath("LEASES/"));
		to.setLeases_lease_used(from.xpath("LEASES/"));
		to.setLeases_lease_vid(from.xpath("LEASES/"));

		to.setErrorMSG(from.info().getErrorMessage());
		return to;
	}

	/**
	 * publish vnet
	 * 
	 * @param client
	 * @param vnetId
	 * @return
	 * @throws Exception
	 */
	public String publish(String secret, String endpoint, int vnetId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = VirtualNetwork.publish(client, vnetId, true);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

	/**
	 * unpublish vnet
	 * 
	 * @param client
	 * @param vnetId
	 * @return
	 * @throws Exception
	 */
	public String unpublish(String secret, String endpoint, int vnetId) throws Exception {
		Client client = new Client(secret, endpoint);
		OneResponse res = VirtualNetwork.publish(client, vnetId, false);
		return res.isError() ? res.getErrorMessage() : res.getMessage();
	}

}
